package com.lianxian.xindong.life.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lianxian.xindong.life.R;
import com.lianxian.xindong.life.R2;
import com.lianxian.xindong.life.entities.ComboModel;
import com.lianxian.xindong.life.util.GlideUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ComboAdapter extends RecyclerView.Adapter {
    List<ComboModel> list;
    Context context;

    public ComboAdapter(List<ComboModel> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.item_combo, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        ComboModel comboModel = list.get(position);
        GlideUtil.GlideWithPlaceHolder(context, comboModel.getComboPicUrl()).into(viewHolder.headPicIV);
        viewHolder.openDaysTV.setText(comboModel.getOpenDay());
        if (comboModel.isOverlayOrNot()) {
            viewHolder.participationTV.setText("可以叠加");
        } else {
            viewHolder.participationTV.setText("不可叠加");
        }
        if (comboModel.isReserveOrNot()) {
            viewHolder.reserveTV.setText("需要预约");
        } else {
            viewHolder.reserveTV.setText("免预约");
        }
        viewHolder.priceTV.setText("¥" + comboModel.getPrice());
        viewHolder.discountTV.setText(comboModel.getDiscount() + "折");
        viewHolder.leftCountTV.setText("仅剩" + comboModel.getLeftCount() + "张");
        viewHolder.leftTimeTV.setText("剩余" + comboModel.getLeftTime());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.combo_head_pic)
        ImageView headPicIV;
        @BindView(R2.id.combo_title)
        TextView titleTV;
        @BindView(R2.id.combo_open_days)
        TextView openDaysTV;
        @BindView(R2.id.participation_or_not)
        TextView participationTV;
        @BindView(R2.id.reserve_or_not)
        TextView reserveTV;
        @BindView(R2.id.price)
        TextView priceTV;
        @BindView(R2.id.discount)
        TextView discountTV;
        @BindView(R2.id.left_count)
        TextView leftCountTV;
        @BindView(R2.id.left_time)
        TextView leftTimeTV;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
